const mongoose = require('mongoose');

const ProgressSchema = new mongoose.Schema({
  user: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User',
    required: true
  },
  challenge: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Challenge',
    required: true
  },
  pattern: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Pattern',
    required: true
  },
  level: {
    type: String,
    required: true,
    enum: ['beginner', 'intermediate', 'advanced']
  },
  status: {
    type: String,
    required: true,
    enum: ['not_started', 'in_progress', 'submitted', 'completed'],
    default: 'not_started'
  },
  attempts: {
    type: Number,
    default: 0
  },
  lastSubmission: {
    code: String,
    submittedAt: Date
  },
  evaluation: {
    rating: {
      type: String,
      enum: ['Unsatisfactory', 'Satisfactory', 'Good', 'Excellent']
    },
    feedback: String,
    suggestions: String,
    correctPatternUsage: Boolean,
    evaluatedAt: Date
  },
  timeSpent: {
    type: Number,  // in seconds
    default: 0
  },
  hintsUsed: {
    type: Number,
    default: 0
  },
  completed: {
    type: Boolean,
    default: false
  },
  completedAt: {
    type: Date
  }
}, {
  timestamps: true
});

// Create a compound index for user and challenge
ProgressSchema.index({ user: 1, challenge: 1 }, { unique: true });

module.exports = mongoose.model('Progress', ProgressSchema); 